package com.tyk.bookstore.front.product.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tyk.bookstore.front.common.core.util.AssertUtil;
import com.tyk.bookstore.front.common.core.util.ObjUtil;
import com.tyk.bookstore.front.product.model.dto.Book4CartDto;
import com.tyk.bookstore.front.product.model.dto.Book4OrderCommentSuggestionDto;
import com.tyk.bookstore.front.product.model.dto.Book4OrderDto;
import com.tyk.bookstore.front.product.model.dto.BookDto;
import com.tyk.bookstore.front.product.model.entity.BookDo;
import com.tyk.bookstore.front.product.model.enume.BookState;
import com.tyk.bookstore.front.product.model.query.Book4OrderPriceCheckQuery;
import com.tyk.bookstore.front.product.model.result.BookResultEnum;
import com.tyk.bookstore.front.product.repository.BookRepository;
import com.tyk.bookstore.front.product.service.BookApiService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author tyk
 * @description 书籍对内暴露接口
 */
@Service
@RequiredArgsConstructor
public class BookApiServiceImpl implements BookApiService {

    private final BookRepository bookRepository;

    @Override
    public BookDto getBookBriefById(Long id) {
        BookDto book = bookRepository.selectBookDtoById(id);
        AssertUtil.notNull(book, BookResultEnum.BOOK_NOT_FOUND);
        return book;
    }

    @Override
    public String getNameById(Long id) {
        String name = bookRepository.selectNameById(id);
        AssertUtil.notBlank(name, BookResultEnum.BOOK_NOT_FOUND);
        return name;
    }

    @Override
    public List<Long> getIdListByNameLike(String name) {
        return bookRepository.selectIdListByNameLike(name);
    }

    @Override
    public Book4CartDto getBook4CartDtoById(Long id) {
        Book4CartDto book = bookRepository.selectBook4CartDtoById(id);
        AssertUtil.notNull(book, BookResultEnum.BOOK_NOT_FOUND);
        return book;
    }

    @Override
    public List<Book4OrderDto> getBook4OrderDtoListByIdSet(Set<Long> idSet) {
        if (idSet.isEmpty()) return new ArrayList<>();
        List<Book4OrderDto> dtoList = bookRepository.selectBook4OrderDtoListByIdSet(idSet);
        AssertUtil.equal(dtoList.size(), idSet.size(), BookResultEnum.BOOK_NOT_FOUND);
        return dtoList;
    }

    @Override
    public void assertExistByIdSet(Set<Long> idSet) {
        if (idSet.isEmpty()) return;
        AssertUtil.equal(
                bookRepository.count1In1Eq(
                        BookDo::getId, idSet,
                        BookDo::getState, BookState.ON,
                        BookDo.class
                ),
                (long) idSet.size(),
                BookResultEnum.BOOK_NOT_FOUND_OR_OFF
        );
    }

    @Override
    public void assertExistById(Long id) {
        AssertUtil.isTrue(
                bookRepository.exist2Eq(
                        BookDo::getId, id,
                        BookDo::getState, BookState.ON,
                        BookDo.class
                ),
                BookResultEnum.BOOK_NOT_FOUND_OR_OFF
        );
    }

    @Override
    public void assertPriceNotChange(List<Book4OrderPriceCheckQuery> queryList) {
        Long count = bookRepository.countByIdAndPrice(queryList);
        AssertUtil.equal(count, Long.valueOf(queryList.size()), BookResultEnum.BOOK_PRICE_CHANGE);
    }

    @Override
    public List<Book4CartDto> getBook4CartDtoListByIdSet(Set<Long> idSet) {
        if (idSet.isEmpty()) return new ArrayList<>();
        // 不对书籍是否存在进行校验
        return bookRepository.selectBook4CartDtoListByIdSet(idSet, idSet.size());
    }

    @Override
    public List<Book4OrderCommentSuggestionDto> getBook4OrderCommentSuggestionDtoListByIdSet(Set<Long> idSet) {
        if (idSet.isEmpty()) return new ArrayList<>();
        List<Book4OrderCommentSuggestionDto> dtoList = bookRepository.selectBook4OrderCommentSuggestionDtoListByIdSet(idSet);
        AssertUtil.equal(dtoList.size(), idSet.size(), BookResultEnum.BOOK_NOT_FOUND);
        return dtoList;
    }

    @Override
    public List<Long> getOffIdListByIdSet(Set<Long> idSet) {
        if (ObjUtil.isEmpty(idSet)) return new ArrayList<>();
        LambdaQueryWrapper<BookDo> w = Wrappers.lambdaQuery(BookDo.class);
        w.select(BookDo::getId)
                .eq(BookDo::getState, BookState.OFF)
                .in(BookDo::getId, idSet);
        return bookRepository.selectList(w)
                .stream().map(BookDo::getId).toList();
    }

}
